package com.ld.admin.webnotice.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.corundumstudio.socketio.SocketIOClient;
import com.ld.admin.webnotice.model.SocketIOClientInfo;
import com.ld.common.service.ProjectBasicService;
import com.ld.common.webnotice.socket.SocketIOServerUtil;
import com.ld.shieldsb.common.web.model.SysUser;
import com.ld.shieldsb.dao.model.QueryModel;

/**
 * 
 * socketio服务类，@Service注解用于自动注入
 * 
 * @ClassName SocketIOService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年02月21日 14:32:08
 *
 */
@Service
public class SocketIOService extends ProjectBasicService {

    /**
     * 获取客户端列表
     * 
     * @Title getList
     * @author 吕凯
     * @date 2019年7月11日 下午2:55:59
     * @param request
     * @param queryModel
     * @return List<SocketIOClientInfo>
     */
    public List<SocketIOClientInfo> getList(HttpServletRequest request, QueryModel queryModel) {
        List<SocketIOClientInfo> list = new ArrayList<>();
        Map<String, Map<String, SocketIOClient>> clientMap = SocketIOServerUtil.getClientMap();
        if (clientMap != null && !clientMap.isEmpty()) {
            Set<String> keys = clientMap.keySet();
            for (String key : keys) {
                Map<String, SocketIOClient> map = clientMap.get(key);
                if (map != null && !map.isEmpty()) {
                    Set<String> usersKeys = map.keySet();
                    for (String usersKey : usersKeys) {
                        SocketIOClient client = map.get(usersKey);
                        SocketIOClientInfo clientInfo = new SocketIOClientInfo();
                        SysUser user = getBaseDAO().findById(SysUser.class, key);
                        if (user != null) {
                            clientInfo.setUname(user.getUserName());
                        }
                        SocketIOServerUtil.fillClientInfo(clientInfo, key, usersKey, client);
                        list.add(clientInfo);
                    }
                }
            }
        }
        // 排序
        Collections.sort(list, new Comparator<SocketIOClientInfo>() {
            @Override
            public int compare(SocketIOClientInfo c1, SocketIOClientInfo c2) {
                int diff = c1.getUid().compareTo(c2.getUid());
                if (diff > 0) {
                    return 1;
                } else if (diff < 0) {
                    return -1;
                }
                return 0; // 相等为0
            }
        }); // 从小到大
        setRequestAttriList(request, list);
        return list;
    }

    /**
     * 获取单个客户端信息
     * 
     * @Title getOne
     * @author 吕凯
     * @date 2019年7月11日 下午2:55:45
     * @param request
     * @param id
     * @return SocketIOClientInfo
     */
    public SocketIOClientInfo getOne(HttpServletRequest request, String id) {
        SocketIOClientInfo clientInfo = new SocketIOClientInfo();
        SocketIOClient client = SocketIOServerUtil.getClientById(id);
        if (client != null) {
            SysUser user = getBaseDAO().findById(SysUser.class, client.get("uid"));
            if (user != null) {
                clientInfo.setUname(user.getUserName());
            }
            SocketIOServerUtil.fillClientInfo(clientInfo, client.get("uid"), client.getSessionId().toString(), client);
        }
        setRequestAttriModel(request, clientInfo);
        return clientInfo;
    }

}
